package com.mangowang.module.main.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.WebApis;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.Merchant;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.module.main.web.ui.WebViewActivity;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.mangowang.module.main.adapter.ShopNewNextAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShopNewNextActivity extends BaseActivity {

    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    private ShopNewNextAdapter mAdapter;
    private List<Merchant.merchant_info> datas = new ArrayList<>();

    @BindView(R2.id.et_search)
    EditText etSearch;

    private String searchKey = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_new_next);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#FFFFFF"), true);
        initRecyclerView();
        initListener();
        tvTitle.setText(getIntent().getStringExtra("title"));
        loadData();



    }

    private void initListener() {
        backIv.setOnClickListener(view -> finish());

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchKey = s.toString();
                if (TextUtils.isEmpty(searchKey)) {
                    searchKey = "";
                }
                loadData();
            }


            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void loadData() {
        showProgress();
        Merchant.get_merchant_list.Builder builder = Merchant.get_merchant_list.newBuilder();
        builder.setType(getIntent().getStringExtra("type"));
        builder.setPattern(getIntent().getStringExtra("pattern"));
        builder.setSnCode(searchKey);
        NetworkManager.INSTANCE.post(Apis.get_merchant_list,builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                hideProgress();
                Merchant.get_merchant_list data = Merchant.get_merchant_list.parseFrom(response);
                List<Merchant.merchant_info> lists = data.getListList();
                Log.e("TAG", "onOk: "+lists.toString());
                mAdapter.setNewInstance(lists);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
//                showToast(errorMessage);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new ShopNewNextAdapter(mContext, datas));
        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            WebViewActivity.startActivity(mContext, WebApis.merchant_detail + "?sn_code="+mAdapter.getData().get(position).getSnCode());
        });
    }


}
